package com.nineclock.system.service.impl;

import com.aliyun.oss.OSS;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nineclock.common.entity.Result;
import com.nineclock.common.entity.UserInfo;
import com.nineclock.common.enums.ResponseEnum;
import com.nineclock.common.exception.NcException;
import com.nineclock.common.oss.OssProperties;
import com.nineclock.common.utils.CurrentUserHolder;
import com.nineclock.system.dto.SysCompanyDTO;
import com.nineclock.system.mapper.SysCompanyMapper;
import com.nineclock.system.pojo.SysCompany;
import com.nineclock.system.service.SysCompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.UUID;


@Service
public class SysCompanyServiceImpl extends ServiceImpl<SysCompanyMapper, SysCompany> implements SysCompanyService {

    @Override
    public SysCompany findList() {
        UserInfo userInfo = CurrentUserHolder.get();
        if (userInfo==null){
            throw new NcException(ResponseEnum.ERROR);
        }
        Long companyId = userInfo.getCompanyId();//当前企业id
        SysCompany sysCompany = this.getById(companyId);
        if (sysCompany==null){
            throw new NcException(ResponseEnum.DATA_NOT_EXISTS);
        }
        return sysCompany;
    }


    @Autowired
    private OSS ossClient;

    @Autowired
    private OssProperties ossProperties;

    @Override
    public Result uploadOSS(MultipartFile file) {
        if (file.isEmpty()){
            throw new NcException(ResponseEnum.INVALID_FILE_TYPE);
        }
        //文件原始名
        String originalFilename = file.getOriginalFilename();
        //文件前缀名
        String fileNamePrefix = UUID.randomUUID().toString().replace("-","");
        int lastIndex = originalFilename.lastIndexOf(".");
        //文件后缀名
        String flieNamePostfix = originalFilename.substring(lastIndex);

        String fileName=fileNamePrefix+flieNamePostfix;
        try {
            InputStream inputStream = file.getInputStream();
            ossClient.putObject(ossProperties.getBucketName(),fileName,inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
        String url = ossProperties.getHost().replace("%s", ossProperties.getBucketName()) + fileName;

        return Result.success(url);
    }


    @Override
    public Result update(SysCompanyDTO dto) {
        if (dto==null){
            throw new NcException(ResponseEnum.INVALID_PARAM_ERROR);
        }
        if (CurrentUserHolder.get()==null){
            return Result.errorMessage(ResponseEnum.ERROR.getMessage());
        }

        Long companyId = CurrentUserHolder.get().getCompanyId();

        SysCompany company = this.getById(companyId);
        if (company==null){
            return Result.errorMessage(ResponseEnum.DATA_NOT_EXISTS.getMessage());
        }
        SysCompany sysCompany = new SysCompany();
        sysCompany.setName(dto.getName());
        sysCompany.setId(companyId);
        sysCompany.setLogo(dto.getLogo());
        sysCompany.setCreateTime(new Date());

        this.updateById(sysCompany);

        return Result.success();
    }
}
